package com.itboye.ihomebank.activity.key;

import android.content.Intent;
import android.graphics.ColorMatrixColorFilter;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.ihomebank.KeyFragment;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.ttKey.Key;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.TimesUtils;

import static com.itboye.ihomebank.KeyFragment.mTTLockAPI;
import static com.itboye.ihomebank.util.MyGongJv.setButtonFocusChanged;

/**
 * 普通用户
 */
public class ActivityPuTongKey extends BaseOtherActivity{
    View v_statusbar;
    ImageView putongkey_finish;//返回
    TextView putongkey_title;//标题
    ImageButton putong_kaisuo;

    private Key localKey;
    LinearLayout bottom,shijian;

    TextView shijain_start,shijain_end;

    // 发送钥匙，发送密码，用户管理，开锁记录
    LinearLayout key_send_yaoshi, key_send_pass, key_guanli, key_jilu;

    @Override
    public void onClick(View v) {
        Intent intent=null;
        switch (v.getId()) {
            case R.id.putongkey_finish:
                finish();
                break;
            case R.id.putong_kaisuo:
              //  showProgressDialog("正在开锁...",true);
              //  mTTLockAPI.connect(localKey.getLockMac());
             //   mTTLockAPI.unlockByUser(null, Integer.parseInt(SPUtils.get(this, null, SPContants.USER_ID, "") + ""), localKey.getLockVersion(), localKey.getStartDate()*1000, localKey.getEndDate()*1000, localKey.getUnlockKey(), localKey.getLockFlagPos(), localKey.getAesKeystr(), localKey.getTimezoneRawOffset());

                String openid = String.valueOf(SPUtils.get(this, null, SPContants.OPENID, ""));
                if (KeyFragment.mTTLockAPI.isConnected(localKey.getLockMac())) {//当前处于连接状态 直接发指令
                    showProgressDialog("正在开锁...", true);
                    if (localKey!=null){
                        if (localKey.getLockVersion()==null||localKey.getUnlockKey()==null||localKey.getAesKeystr()==null||localKey.getAesKeystr().equals("")){
                            ByAlert.alert("需要重新绑定管理员");
                            try {
                                closeProgressDialog();
                            } catch (Exception e) {
                            }
                            return;
                        }
                        mTTLockAPI.unlockByUser(null, openid == null || openid.equals("") ? Integer.parseInt(SPUtils.get(this, null, SPContants.USER_ID, "") + "") : Integer.parseInt(SPUtils.get(this, null, SPContants.OPENID, "") + ""), localKey.getLockVersion(), localKey.getStartDate()*1000, localKey.getEndDate()*1000, localKey.getUnlockKey(), localKey.getLockFlagPos(), localKey.getAesKeystr(), localKey.getTimezoneRawOffset());
                    }else{
                        try {
                            closeProgressDialog();
                        } catch (Exception e) {
                        }
                        ByAlert.alert("需要重新绑定管理员");
                    }
                } else {//未连接进行连接
                    showProgressDialog("等待开锁...", true);
//                    mTTLockAPI.connect(localKey.getLockMac());
//                    bleSession.setOperation(Operation.CLICK_UNLOCK);
//                    bleSession.setLockmac(localKey.getLockMac());
                    mTTLockAPI.connect(localKey.getLockMac());
                    if (localKey!=null){
                        if (localKey.getLockVersion()==null||localKey.getUnlockKey()==null||localKey.getAesKeystr()==null||localKey.getAesKeystr().equals("")){
                            ByAlert.alert("需要重新绑定管理员");
                            try {
                                closeProgressDialog();
                            } catch (Exception e) {
                            }
                            return;
                        }
                        mTTLockAPI.unlockByUser(null, openid == null || openid.equals("") ? Integer.parseInt(SPUtils.get(this, null, SPContants.USER_ID, "") + "") : Integer.parseInt(SPUtils.get(this, null, SPContants.OPENID, "") + ""), localKey.getLockVersion(), localKey.getStartDate()*1000, localKey.getEndDate()*1000, localKey.getUnlockKey(), localKey.getLockFlagPos(), localKey.getAesKeystr(), localKey.getTimezoneRawOffset());
                    }else{
                        try {
                            closeProgressDialog();
                        } catch (Exception e) {
                        }
                        ByAlert.alert("需要重新绑定管理员");
                    }
                }
                break;
            case R.id.key_send_yaoshi:// 发送钥匙
                intent = new Intent(this, ActivitySendKey.class);
                intent.putExtra("lockid", localKey.getLockId());
                startActivity(intent);
                break;
            case R.id.key_send_pass:// 发送密码
                intent = new Intent(this, ActivityFaSongPass.class);
                intent.putExtra("lockid",  localKey.getLockId());
                intent.putExtra("lockName",localKey.getLockAlias());
                startActivity(intent);
                break;
            case R.id.key_guanli:// 用户管理
                ByAlert.alert("授权用户无法使用该功能");
                break;
            case R.id.key_jilu:// 开锁记录
                ByAlert.alert("授权用户无法使用该功能");
                break;

            default:
                break;
        }

    }

    @Override
    protected int layout_id() {
        // TODO Auto-generated method stub
        return R.layout.activity_puong_key;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        localKey = KeyFragment.curKeys;
        putongkey_title.setText(localKey.getLockAlias());
        String start= String.valueOf(localKey.getStartDate());
        String end= String.valueOf(localKey.getEndDate());
        String type= localKey.getUser_type();
        if (type!=null){
            if (type.equals("1")){
                bottom.setVisibility(View.VISIBLE);
            }else{
                bottom.setVisibility(View.GONE);
            }
        }
        if (start!=null&&end!=null){
            if (start.equals("0")){
                shijian.setVisibility(View.GONE);
            }else{
                shijian.setVisibility(View.VISIBLE);
                shijain_start.setText(TimesUtils.timeStamp2Date(Long.parseLong(start)));
                shijain_end.setText(TimesUtils.timeStamp2Date(Long.parseLong(end)));
            }
        }
        setButtonFocusChanged(putong_kaisuo);
    }

    @Override
    protected void onReadIntent(Bundle bundle) {
    }

}
